﻿using System;
using System.ComponentModel;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Telerik.Web.UI;

namespace RadGridDemo
{
    [ToolboxItemAttribute(false)]
    public partial class AdventureWorksCustomers : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
        // the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public AdventureWorksCustomers()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void rgCustomers_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            rgCustomers.DataSource = CustomerHelper.getCustomers();
        }

        protected void rgCustomers_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem gdi = (GridDataItem)(e.Item);

                string login = gdi["SalesPerson"].Text;
                gdi["SalesPerson"].ToolTip = login;
                login = login.Substring(login.IndexOf(@"\")+1);
                gdi["SalesPerson"].Text = login;

                ImageButton delbutton = gdi["DeleteColumnButton"].Controls[0] as ImageButton;
                string customerName = gdi["FirstName"].Text + " " + gdi["LastName"].Text;
                delbutton.ToolTip = "Delete " + customerName + ".";
                delbutton.Attributes["onclick"] = "return confirm('Are you sure you want to delete " + customerName + "?')";


            }

            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                if (!e.Item.OwnerTableView.IsItemInserted)//only do this when editing, not adding a row.
                {
                    RadTextBox salesPersonLogin = (RadTextBox)e.Item.FindControl("radtboxSalesperson");
                    DataRowView dr = (DataRowView)(e.Item.DataItem);
                    string login = dr["SalesPerson"].ToString();
                    login = login.Substring(login.IndexOf(@"\") + 1);
                    salesPersonLogin.Text = login;
                }
            }

        }

        protected void rgCustomers_InsertCommand(object sender, GridCommandEventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                string title = string.Empty;
                string firstName = ((RadTextBox)e.Item.FindControl("radtboxFirstName")).Text;
                string lastName = ((RadTextBox)e.Item.FindControl("radtboxLastName")).Text;
                string company = ((RadTextBox)e.Item.FindControl("radtboxCompany")).Text;
                string salesPerson = "adventure-works\\" + ((RadTextBox)e.Item.FindControl("radtboxSalesperson")).Text;
                string email = ((RadTextBox)e.Item.FindControl("radtboxEmail")).Text;
                string phone = ((RadMaskedTextBox)e.Item.FindControl("radmaskedboxPhone")).Text;
                string address = ((RadTextBox)e.Item.FindControl("radtboxAddress")).Text;
                string city = ((RadTextBox)e.Item.FindControl("radtboxCity")).Text;
                string state = ((RadComboBox)e.Item.FindControl("radcboxState")).Text;
                string country = "United States";
                string zip = ((RadTextBox)e.Item.FindControl("radtboxZip")).Text;


                CustomerHelper.addCustomer(title, firstName,lastName,company,salesPerson,email,phone,
                    address,city,state,country,zip);
     
                
            }
            else
            {
                e.Canceled = true;
            }
        }

        protected void rgCustomers_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            Page.Validate();

            if (Page.IsValid)
            {
                int CustomerID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CustomerID"]);
                
                string title = string.Empty;
                string firstName = ((RadTextBox)e.Item.FindControl("radtboxFirstName")).Text;
                string lastName = ((RadTextBox)e.Item.FindControl("radtboxLastName")).Text;
                string company = ((RadTextBox)e.Item.FindControl("radtboxCompany")).Text;
                string salesPerson = "adventure-works\\" + ((RadTextBox)e.Item.FindControl("radtboxSalesperson")).Text;
                string email = ((RadTextBox)e.Item.FindControl("radtboxEmail")).Text;
                string phone = ((RadMaskedTextBox)e.Item.FindControl("radmaskedboxPhone")).Text;
                string address = ((RadTextBox)e.Item.FindControl("radtboxAddress")).Text;
                string city = ((RadTextBox)e.Item.FindControl("radtboxCity")).Text;
                string state = ((RadComboBox)e.Item.FindControl("radcboxState")).Text;
                string country = "United States";
                string zip = ((RadTextBox)e.Item.FindControl("radtboxZip")).Text;


                CustomerHelper.updateCustomer(CustomerID, title, firstName, lastName, company, salesPerson, email, phone,
                    address, city, state, country, zip);
                

            }
            else
            {
                e.Canceled = true;
            }
        }

        protected void rgCustomers_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            int CustomerID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CustomerID"]);
            CustomerHelper.deleteCustomer(CustomerID);
        }


    }
}
